package com.jiangjiang.eshop.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.order.convert.ReturnGoodsApplyConvert;
import com.jiangjiang.eshop.order.dao.ReturnGoodsApplyMapper;
import com.jiangjiang.eshop.order.domain.ReturnGoodsApplyEntity;
import com.jiangjiang.eshop.order.dto.ReturnGoodsApplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName ReturnGoodsApplyService
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/13 7:49
 * @Version 1.0
 **/
@Service
public class ReturnGoodsApplyService extends ServiceImpl<ReturnGoodsApplyMapper, ReturnGoodsApplyEntity> {

    @Autowired
    private ReturnGoodsApplyConvert returnGoodsApplyConvert;

    /**
     * 根据订单号查询退货申请
     *
     * @param orderInfoId 订单编号
     * @return 查询结果
     */
    public ReturnGoodsApplyDTO queryByOrderInfoId(Long orderInfoId) {
        LambdaQueryWrapper<ReturnGoodsApplyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReturnGoodsApplyEntity::getOrderInfoId, orderInfoId);
        return returnGoodsApplyConvert.convertEntity2DTO(this.getOne(wrapper));
    }

    /**
     * 退货申请保存
     *
     * @param dto 退货申请
     * @return 处理结果
     */
    public Boolean save(ReturnGoodsApplyDTO dto) {
        return this.save(returnGoodsApplyConvert.convertDTO2Entity(dto));
    }

    /**
     * 更新退货物流单号
     *
     * @param orderInfoId             订单id
     * @param returnGoodsLogisticCode 退货物流单号
     */
    public void updateReturnGoodsLogisticCode(Long orderInfoId, String returnGoodsLogisticCode) {
        LambdaUpdateWrapper<ReturnGoodsApplyEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ReturnGoodsApplyEntity::getOrderInfoId, orderInfoId)
                .set(ReturnGoodsApplyEntity::getReturnGoodsLogisticCode, returnGoodsLogisticCode);
        this.update(wrapper);
    }

    /**
     * 退货申请更新状态
     * @param orderId 订单id
     * @param status 状态
     */
    public void updateStatus(Long orderId, Integer status) {
        LambdaUpdateWrapper<ReturnGoodsApplyEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ReturnGoodsApplyEntity::getOrderInfoId, orderId)
                .set(ReturnGoodsApplyEntity::getReturnGoodsApplyStatus, status);
        this.update(wrapper);
    }
}
